package fr.emse.tatiana.analysis.annotation.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import fr.emse.tatiana.Activator;
import fr.emse.tatiana.CorpusNavigationView;
import fr.emse.tatiana.analysis.annotation.categorisation.CategorisationAnalysisInput;
import fr.emse.tatiana.commands.ICommandIds;
import fr.emse.tatiana.corpus.Corpus;
import fr.emse.tatiana.display.BadDisplayFile;
import fr.emse.tatiana.replayable.ReplayableInput;
import fr.emse.tatiana.utils.WSFileDialog;

/**
 * Action to create and open a new Categorisation Analysis with a new categories description
 * 
 * @author dyke
 *
 */
public class NewCategAnalysisAction extends Action {
	private final IWorkbenchWindow window;
	private final String navigationID;
	private final String editorID;

	private static NewCategAnalysisAction instance;

	public static NewCategAnalysisAction getInstance() {
		return instance;
	}

	public NewCategAnalysisAction(IWorkbenchWindow window, String label, String edid, String navid) {
		this.window = window;
		this.navigationID = navid;
		this.editorID = edid;
		setText(label);
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_NEW_CATEGORISATION_ANALYSIS);
		// Associate the action with a pre-defined command, to allow key bindings.
		//setActionDefinitionId(ICommandIds.CMD_NEW_CATEGORISATION_ANALYSIS);
		setImageDescriptor(fr.emse.tatiana.Activator.getImageDescriptor("/icons/sample2.gif"));
		this.setEnabled(false);
		instance = this;
	}


	public void run() {
		if(window != null) {	
			IProject p = ((CorpusNavigationView) window.getActivePage().findView(this.navigationID)).getSelectedCorpus().getProject();
			IEditorInput input = new CategorisationAnalysisInput(p, null);
			try {
				window.getActivePage().openEditor(input, editorID);
			} catch (PartInitException e) {
				new MessageDialog(window.getShell(), 
						"Problem Opening Categorisation Analysis",
						null,
						"Could not open categorisation " + input.getName(),
						MessageDialog.ERROR,
						new String[]{"Ok"},
						0).open();
			}
		}
	}
}